package com.an.demo.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.an.common.core.domain.R;
import com.an.demo.domain.EsDocument;
import com.an.demo.es.esmapper.EsDemoMapper;
import com.an.es.properties.EasyEsProperties;
import lombok.RequiredArgsConstructor;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Es演示控制器
 *
 * @author: Ann
 * @createDate: 2024/03/10
 * @version: 1.0.0
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/es")
@SaIgnore
public class EsDemoController {

    private final EsDemoMapper esDemoMapper;

    private final EasyEsProperties esProperties;

    /**
     * 创建索引
     *
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2024/03/10
     * @version: 1.0.0
     */
    @GetMapping("/createIndex")
    public R<Void> createIndex() {
        esDemoMapper.createIndex();
        return R.ok();
    }

    /**
     * 加
     *
     * @return {@link R }<{@link Integer }>
     * @author Ann
     * @date 2024/06/01
     */
    @GetMapping("/addByEasyEs")
    public R<Integer> add() {
        System.out.println(esProperties.getEnable());
        List<EsDocument> documents = buildData();
        Integer i = esDemoMapper.insertBatch(documents);
        return R.ok(i);
    }

    private List<EsDocument> buildData() {
        List<EsDocument> documents = new ArrayList<>();
        // 创建第一条数据
        EsDocument document1 = new EsDocument();
        document1.setId("124");
        document1.setArticleContent("这是第二篇博客的内容");
        String[] tags1 = {"python", "数据分析", "机器学习"};
        document1.setTags(tags1);

// 创建第二条数据
        EsDocument document2 = new EsDocument();
        document2.setId("125");
        document2.setArticleContent("今天天气真好，适合出去运动");
        String[] tags2 = {"健身", "跑步", "户外运动"};
        document2.setTags(tags2);

// 创建第三条数据
        EsDocument document3 = new EsDocument();
        document3.setId("126");
        document3.setArticleContent("新产品发布会即将召开，敬请期待,放假");
        String[] tags3 = {"新品", "发布会", "科技"};
        document3.setTags(tags3);

// 创建第四条数据
        EsDocument document4 = new EsDocument();
        document4.setId("127");
        document4.setArticleContent("明天放假了，准备去旅行放松一下");
        String[] tags4 = {"度假", "旅游", "休闲"};
        document4.setTags(tags4);
        documents.add(document1);
        documents.add(document2);
        documents.add(document3);
        documents.add(document4);
        return documents;
    }


    /**
     * 选择列表
     *
     * @return {@link R }<{@link List }<{@link EsDocument }>>
     * @author Ann
     * @date 2024/06/04
     */
    @GetMapping("/selectListByEasyEs")
    public R<List<EsDocument>> selectList() {
        List<String> list = Arrays.asList("度假", "科技");
        LambdaEsQueryWrapper<EsDocument> esQueryWrapper = new LambdaEsQueryWrapper<EsDocument>()
            .in(EsDocument::getTags, list).and(wr->wr.like(EsDocument::getArticleContent,"放假"));
        List<EsDocument> documents = esDemoMapper.selectList(esQueryWrapper);
        return R.ok(documents);
    }
    /**
     * 按 ID 更新
     * Easy-Es更新时会忽略null值
     *
     * @return {@link R }<{@link Void }>
     * @author Ann
     * @date 2024/07/08
     */
    @GetMapping("/updateById")
    public R<Void> updateById() {
        EsDocument document4 = new EsDocument();
        document4.setId("127");
        document4.setArticleContent("测试Es更新时对null值的处理");
        String[] tags4 = {"度假"};
        document4.setTags(tags4);
        esDemoMapper.updateById(document4);
        return R.ok();
    }

}
